package com.example.service.impl;

import com.example.domain.entity.ProductionPlan;
import com.example.mapper.ProductionPlanMapper;
import com.example.service.IProductionPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.example.domain.query.ProductionPlanQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 生产计划表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-05-14
 */
@Service
public class ProductionPlanServiceImpl extends ServiceImpl<ProductionPlanMapper, ProductionPlan> implements IProductionPlanService {

    @Override
    public IPage<ProductionPlan> pageQuery(ProductionPlanQuery query, int pageNum, int pageSize) {
        Page<ProductionPlan> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductionPlan> wrapper = new QueryWrapper<>();
        if (query != null) {
            if (query.getPlanId() != null) {
                wrapper.eq("plan_id", query.getPlanId());
            }
            if (StringUtils.hasText(query.getDepartmentName())) {
                wrapper.eq("department_name", query.getDepartmentName());
            }
            if (StringUtils.hasText(query.getProductName())) {
                wrapper.like("product_name", query.getProductName());
            }
            if (query.getPlannedQuantity() != null) {
                wrapper.eq("planned_quantity", query.getPlannedQuantity());
            }
            if (query.getActualQuantity() != null) {
                wrapper.eq("actual_quantity", query.getActualQuantity());
            }
            if (query.getAuditStatus() != null) {
                wrapper.eq("audit_status", query.getAuditStatus());
            }
            if (query.getAuditorId() != null) {
                wrapper.eq("auditor_id", query.getAuditorId());
            }
            if (query.getCreateTimeBegin() != null && query.getCreateTimeEnd() != null) {
                wrapper.between("create_time", query.getCreateTimeBegin(), query.getCreateTimeEnd());
            }
            if (query.getUpdateTimeBegin() != null && query.getUpdateTimeEnd() != null) {
                wrapper.between("update_time", query.getUpdateTimeBegin(), query.getUpdateTimeEnd());
            }
        }
        return page(page, wrapper);
    }
}
